package com.bang.study.consumer;

import com.bang.study.constants.PublicConstants;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;

import java.util.List;

/**
 * @ClassName: BroadCastingConsumer
 * @Desc: TODO
 * @Auther: <Bang>
 * @Date: 2020/05/06 18:22
 * @Version: 1.0
 * @Modified By:
 */
public class BroadCastingConsumer {

    public static void main(String[] args) throws Exception{
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(PublicConstants.TEST_CONSUMER_GROUP);
        consumer.setNamesrvAddr(PublicConstants.NAME_ADDR);
        consumer.setMessageModel(MessageModel.BROADCASTING);
        consumer.subscribe(PublicConstants.TEST_TOPIC,"tag3");
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                for (MessageExt msg : msgs) {
                    byte[] body = msg.getBody();
                    String s = new String(body);
                    System.out.println("消息内容 == " + s);
                    System.out.println("msgs == " + msgs);
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }

        });
        consumer.start();
        System.out.println("消费者启动了==========================");
    }
}
